package com.taobao.api.convert;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.taobao.api.TaobaoApiException;
import com.taobao.api.model.Order;
import com.taobao.api.model.Trade;
import com.taobao.api.model.TradeRate;
import com.taobao.api.util.ApiConstants;
import com.taobao.api.util.DateUtil;

/**
 * @author gaoweibin.tw
 *
 */
public class TaobaoTradeJSONConvert {
	
	/**
	 * 将返回的Json转换为Trade
	 * 
	 * @param json
	 *            JSONObject
	 * @return Trade
	 * @throws TaobaoApiException
	 */
	public static Trade convertJsonToTrade(JSONObject json) throws JSONException {
		Trade newTrade = new Trade();
		if (json.has(ApiConstants.SELLER_NICK)) {
			newTrade.setSellerNick(json.getString(ApiConstants.SELLER_NICK));
		}
		if (json.has(ApiConstants.BUYER_NICK)) {
			newTrade.setBuyerNick(json.getString(ApiConstants.BUYER_NICK));
		}
		if (json.has(ApiConstants.IID)) {
			newTrade.setIid(json.getString(ApiConstants.IID));
		}
		if (json.has(ApiConstants.TITLE)) {
			newTrade.setTitle(json.getString(ApiConstants.TITLE));
		}
		if (json.has(ApiConstants.PRICE)) {
			newTrade.setPrice(json.getString(ApiConstants.PRICE));
		}
		if (json.has(ApiConstants.PIC_PATH)) {
			newTrade.setPicPath(json.getString(ApiConstants.PIC_PATH));
		}
		if (json.has(ApiConstants.NUM)) {
			newTrade.setNum(Integer.parseInt(json.getString(ApiConstants.NUM)));
		}
		if (json.has(ApiConstants.CREATED)) {
			try {
				newTrade.setCreated(DateUtil.strToDate(json.getString(ApiConstants.CREATED)));
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		if (json.has(ApiConstants.TYPE)) {
			newTrade.setType(json.getString(ApiConstants.TYPE));
		}
		if (json.has(ApiConstants.TID)) {
			newTrade.setTid(json.getString(ApiConstants.TID));
		}
		if (json.has(ApiConstants.STATUS)) {
			newTrade.setStatus(json.getString(ApiConstants.STATUS));
		}
		if (json.has(ApiConstants.SELLER_RATE)) {
			newTrade.setSellerRate(json.getString(ApiConstants.SELLER_RATE).equals("true") ? true	: false);
		}
		if (json.has(ApiConstants.BUYER_RATE)) {
			newTrade
					.setBuyerRate(json.getString(ApiConstants.BUYER_RATE).equals("true") ? true
							: false);
		}
		if (json.has(ApiConstants.ALIPAYNO)) {
			newTrade.setAlipayNo(json.getString(ApiConstants.ALIPAYNO));
		}
		if (json.has(ApiConstants.PAYMENT)) {
			newTrade.setPayment(json.getString(ApiConstants.PAYMENT));
		}
		
		if(json.has(ApiConstants.BUYER_MESSAGE)){
			newTrade.setBuyerMessage(json.getString(ApiConstants.BUYER_MESSAGE));
		}
		if(json.has(ApiConstants.PAY_TIME)){
			try {
				newTrade.setPayTime(DateUtil.strToDate(json.getString(ApiConstants.PAY_TIME)));
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		if(json.has(ApiConstants.END_TIME)){
			try {
				newTrade.setEndTime(DateUtil.strToDate(json.getString(ApiConstants.END_TIME)));
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		if(json.has(ApiConstants.MODIFIED)){
			try {
				newTrade.setModified(DateUtil.strToDate(json.getString(ApiConstants.MODIFIED)));
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		if(json.has(ApiConstants.BUYER_OBTAIN_POINT_FEE)){
			newTrade.setBuyerObtainPointFee(Integer.parseInt(json.getString(ApiConstants.BUYER_OBTAIN_POINT_FEE)));
		}
		if(json.has(ApiConstants.POINT_FEE)){
			newTrade.setPointFee(Integer.parseInt(json.getString(ApiConstants.POINT_FEE)));
		}
		if(json.has(ApiConstants.REAL_POINT_FEE)){
			newTrade.setRealPointFee(Integer.parseInt(json.getString(ApiConstants.REAL_POINT_FEE)));
		}
		if(json.has(ApiConstants.SID)){
			newTrade.setSid(json.getString(ApiConstants.SID));
		}
		if(json.has(ApiConstants.OUT_SID)){
			newTrade.setOutSid(json.getString(ApiConstants.OUT_SID));
		}
		if(json.has(ApiConstants.SELLER_MEMO)){
			newTrade.setSellerMemo(json.getString(ApiConstants.SELLER_MEMO));
		}
		if(json.has(ApiConstants.BUYER_MEMO)){
			newTrade.setBuyerMemo(json.getString(ApiConstants.BUYER_MEMO));
		}
		if(json.has(ApiConstants.TOTAL_FEE)){
			newTrade.setTotalFee(json.getString(ApiConstants.TOTAL_FEE));
		}
		if(json.has(ApiConstants.ORDERS)){
			newTrade.setOrders(convertJsonArrayToOrderList(json.getJSONArray(ApiConstants.ORDERS)));
		}
		return newTrade;
	}
	
	/**
	 * 将返回的JsonArray转换为List<code><</code>TradeRate>
	 * 
	 * @param jsonArray
	 *            Json数组
	 * @return List<code><</code>TradeRate>
	 * @throws TaobaoApiException
	 */
	public static List<TradeRate> convertJsonArrayToTradeRateList(JSONArray jsonArray) {
		List<TradeRate> getTradeRateList = new ArrayList<TradeRate>();
		TradeRate getTradeRate = null;
		for (int i = 0; i < jsonArray.length(); i++) {
			try {
				getTradeRate = convertJsonToTradeRate(jsonArray.getJSONObject(i));
			} catch (JSONException e) {
				e.printStackTrace();
			}
			getTradeRateList.add(getTradeRate);
		}
		return getTradeRateList;
	}
	
	/**
	 * 将返回的Json转换为TradeRate
	 * 
	 * @param json
	 *            JSONObject
	 * @return TradeRate
	 * @throws TaobaoApiException
	 */
	public static TradeRate convertJsonToTradeRate(JSONObject json) throws JSONException {
		TradeRate getTradeRate = new TradeRate();
		if (json.has(ApiConstants.TID)) {
			getTradeRate.setTid(json.getString(ApiConstants.TID));
		}
		if (json.has(ApiConstants.ROLE)) {
			getTradeRate.setRole(json.getString(ApiConstants.ROLE));
		}
		if (json.has(ApiConstants.RATED_NICK)) {
			getTradeRate.setRatedNick(json.getString(ApiConstants.RATED_NICK));
		}
		if (json.has(ApiConstants.NICK)) {
			getTradeRate.setNick(json.getString(ApiConstants.NICK));
		}
		if (json.has(ApiConstants.RESULT)) {
			getTradeRate.setResult(json.getString(ApiConstants.RESULT));
		}
		if (json.has(ApiConstants.CREATED)) {
			try {
				getTradeRate.setCreated(DateUtil.strToDate(json.getString(ApiConstants.CREATED)));
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		if (json.has(ApiConstants.ITEM_TITLE)) {
			getTradeRate.setItemTitle(json.getString(ApiConstants.ITEM_TITLE));
		}
		if (json.has(ApiConstants.ITEM_PRICE)) {
			getTradeRate.setItemPrice(json.getString(ApiConstants.ITEM_PRICE));
		}
		if (json.has(ApiConstants.CONTENT)) {
			getTradeRate.setContent(json.getString(ApiConstants.CONTENT));
		}
		if (json.has(ApiConstants.REPLY)) {
			getTradeRate.setReply(json.getString(ApiConstants.REPLY));
		}
		return getTradeRate;
	}
	
	/**
	 * 将返回的JsonArray转换为List<code><</code>Trade>
	 * 
	 * @param jsonArray
	 *            Json数组
	 * @return List<code><</code>Trade>
	 * @throws TaobaoApiException
	 */
	public static List<Trade> convertJsonArrayToTradeList(JSONArray jsonArray) {
		List<Trade> getTradesList = new ArrayList<Trade>();
		Trade getTrade = null;
		for (int i = 0; i < jsonArray.length(); i++) {
			try {
				getTrade = convertJsonToTrade(jsonArray.getJSONObject(i));
			} catch (JSONException e) {
				e.printStackTrace();
			}
			getTradesList.add(getTrade);
		}
		return getTradesList;
	}
	
	public static List<Order> convertJsonArrayToOrderList(JSONArray jsonArray){
		List<Order> getOrderList = new ArrayList<Order>();
		Order getOrder = null;
		for (int i = 0; i < jsonArray.length(); i++) {
			try {
				getOrder = convertJsonToOrder(jsonArray.getJSONObject(i));
			} catch (JSONException e) {
				e.printStackTrace();
			}
			getOrderList.add(getOrder);
		}
		return getOrderList;
	}
	
	public static Order convertJsonToOrder(JSONObject json) throws JSONException{
		Order order = new Order();
		if(json.has(ApiConstants.SELLER_NICK)){
			order.setSellerNick(json.getString(ApiConstants.SELLER_NICK));
		}
		if(json.has(ApiConstants.BUYER_NICK)){
			order.setBuyerNick(json.getString(ApiConstants.BUYER_NICK));
		}
		if(json.has(ApiConstants.TYPE)){
			order.setType(json.getString(ApiConstants.TYPE));
		}
		if(json.has(ApiConstants.CREATED)){
			try {
				order.setCreated(DateUtil.strToDate(json.getString(ApiConstants.CREATED)));
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		if(json.has(ApiConstants.IID)){
			order.setIid(json.getString(ApiConstants.IID));
		}
		if(json.has(ApiConstants.SKU_ID)){
			order.setSkuId(json.getString(ApiConstants.SKU_ID));
		}
		if(json.has(ApiConstants.NUM)){
			order.setNum(Integer.parseInt(json.getString(ApiConstants.NUM)));
		}
		if(json.has(ApiConstants.TITLE)){
			order.setTitle(json.getString(ApiConstants.TITLE));
		}
		if(json.has(ApiConstants.PIC_PATH)){
			order.setPicPath(json.getString(ApiConstants.PIC_PATH));
		}
		if(json.has(ApiConstants.PRICE)){
			order.setPrice(json.getString(ApiConstants.PRICE));
		}
		if(json.has(ApiConstants.TID)){
			order.setTid(json.getString(ApiConstants.TID));
		}
		if(json.has(ApiConstants.REFUND_STATUS)){
			order.setRefundStatus(json.getString(ApiConstants.REFUND_STATUS));
		}
		return order;
	}

	public static List<Order> convertJsonToOrderList(JSONArray orders) {
		List<Order> getOrdersList = new ArrayList<Order>();
		Order getOrder = null;
		for (int i = 0; i < orders.length(); i++) {
			try {
				getOrder = convertJsonToOrder(orders.getJSONObject(i));
			} catch (JSONException e) {
				e.printStackTrace();
			}
			getOrdersList.add(getOrder);
		}
		return getOrdersList;
	}
}
